package com.xiaoshuidi.cloud.module.finance.mq.consumer;

import cn.hutool.core.collection.CollectionUtil;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.bpm.enums.BpmBusinessTypeEnum;
import com.xiaoshuidi.cloud.module.bpm.mq.message.BpmProcessInstanceResultEvent;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountService;
import com.xiaoshuidi.cloud.module.finance.service.finance.FinanceAccountWithdrawService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class BpmProcessInstanceResultConsumer implements ApplicationListener<BpmProcessInstanceResultEvent> {

    @Resource
    private FinanceAccountWithdrawService financeAccountWithdrawService;
    @Resource
    private FinanceAccountService financeAccountService;

    @Override
    public void onApplicationEvent(BpmProcessInstanceResultEvent event) {
        log.info("收到工作流状态变更消息{}", JsonUtils.toJsonString(event));
        //是优惠券的消息才处理
        if (BpmBusinessTypeEnum.BALANCE_WITHDRAWAL.name().equals(event.getBusinessType())) {
            log.info("处理审批结果 {} {}", event.getBusinessKey(), event.getResult());
            financeAccountWithdrawService.approvalResult(event);
        }
        //个人余额提现 获企业余额提现
        if (CollectionUtil.newArrayList(BpmBusinessTypeEnum.PERSONAL_WALLET_WITHDRAWAL.name(),
                BpmBusinessTypeEnum.COMPANY_WALLET_WITHDRAWAL.name()
        ).contains(event.getBusinessType())) {
            log.info("个人余额提现/企业余额提现处理审批结果 {} {}", event.getBusinessKey(), event.getResult());
            financeAccountService.approvalWalletRefundResult(event);
        }

    }
}
